{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6dd84091",
   "metadata": {},
   "source": [
    "数据处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab5a7f44",
   "metadata": {},
   "source": [
    "数据来源：https://itrust.sutd.edu.sg/itrust abs_datasets/dataset_info/ \n",
    "数据集：WADI.A1_9 Oct 2017"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c7a23f8",
   "metadata": {},
   "source": [
    "导入库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "911733e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ecd25cb",
   "metadata": {},
   "source": [
    "数据平滑、标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ccd2677",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 读取原始 CSV 文件\n",
    "csv_file_path = 'WADI_eval.csv'\n",
    "df = pd.read_csv(csv_file_path)\n",
    "\n",
    "# 2. 读取 list.txt 文件并获取传感器名称列表\n",
    "with open('list.txt', 'r') as file:\n",
    "    sensors_to_standardize = [line.strip() for line in file if line.strip()]\n",
    "\n",
    "# 检查传感器名称是否存在于 CSV 的列中\n",
    "sensors_to_standardize = [sensor for sensor in sensors_to_standardize if sensor in df.columns]\n",
    "if not sensors_to_standardize:\n",
    "    raise ValueError(\"没有找到eval在 list.txt 中列出的传感器名称。请检查 list.txt 和 CSV 文件。\")\n",
    "\n",
    "# 3. 选择需要标准化的列\n",
    "data_to_standardize = df[sensors_to_standardize]\n",
    "\n",
    "# 新增下采样步骤：每10个数据点取平均\n",
    "# 使用groupby配合整数除法创建分组索引\n",
    "downsampled_data = data_to_standardize.groupby(data_to_standardize.index // 10).mean()\n",
    "\n",
    "# 4. 应用StandardScaler（此时处理的是下采样后的数据）\n",
    "scaler = StandardScaler()\n",
    "standardized_data = scaler.fit_transform(downsampled_data)\n",
    "\n",
    "# 将标准化后的数据转换为 DataFrame，并使用新的列名\n",
    "standardized_df = pd.DataFrame(standardized_data, columns=[f\"{sensor}\" for sensor in sensors_to_standardize])\n",
    "\n",
    "# 5. 保存标准化后的数据到新的 CSV 文件\n",
    "output_csv_file_path = 'eval.csv'\n",
    "standardized_df.to_csv(output_csv_file_path, index=False)\n",
    "\n",
    "print(f\"标准化后的数据已保存到 {output_csv_file_path}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e7332a8",
   "metadata": {},
   "source": [
    "如果是测试集（也是本实验的验证集eval.csv），需要插入attack-label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c17ccc8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取原始文件\n",
    "df = pd.read_csv('WADI_eval.csv')\n",
    "\n",
    "# 提取attack label列数据（确保列名完全匹配）\n",
    "attack_labels = df[['Attack LABEL']].copy()\n",
    "\n",
    "# 保存到新CSV文件\n",
    "attack_labels.to_csv('WADI_attack_labels.csv', index=False)\n",
    "\n",
    "existing_df = pd.read_csv('eval.csv')\n",
    "merged_df = pd.concat([existing_df, attack_labels], axis=1)\n",
    "merged_df.to_csv('merged_file.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1887d2b3",
   "metadata": {},
   "source": [
    "处理结果存于model文件夹下的eval.csv和train.csv文件中"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
